import { ShapeCustomizationManagementModel } from "@/modules/model/modals/shapeCustomization/ShapeCustomizationManagementModel";
import { PropertyKey } from "@/constants/Enums";
import { modelService, shapeService } from "@/modules/model/service";

export type Attribute = {
  id: string;
  name: string;
  hidden: boolean;
};

export type CustomAttributeConfig = {
  enabledComp: boolean;
  customAttributes: CustomAttributes[];
};

export type CustomAttributes = {
  targetName: string;
  targetModelId: string;
  attributes: Attribute[];
};

export class ShapeCompCustomAttrConfigModel {
  customAttributeConfig: CustomAttributeConfig = { enabledComp: false, customAttributes: [] };
  constructor(public shapeCustomizationManagementModel: ShapeCustomizationManagementModel) {
    const row =
      this.shapeCustomizationManagementModel.queryParams.row && JSON.parse(this.shapeCustomizationManagementModel.queryParams.row);
    const shapeConfig = row.value && JSON.parse(row.value);
    if (shapeConfig?.shapeCompartmentConfig?.shapeCompCustomAttrConfig) {
      (async () => {
        try {
          await this.initCustomAttributes(true);
          await this.freshCustomAttributes(shapeConfig.shapeCompartmentConfig.shapeCompCustomAttrConfig);
        } catch (e) {
          throw new Error("freshCustomAttributes fail");
        }
      })();
    } else {
      (async () => {
        try {
          await this.initCustomAttributes(false);
        } catch (e) {
          throw new Error("initCustomAttributes fail");
        }
      })();
    }
  }

  async freshCustomAttributes(shapeCompCustomAttrConfig: CustomAttributeConfig) {
    this.customAttributeConfig.enabledComp = shapeCompCustomAttrConfig.enabledComp;
    shapeCompCustomAttrConfig.customAttributes.forEach(it => {
      const curAttributes = this.customAttributeConfig.customAttributes.find(c => c.targetModelId === it.targetModelId);
      if (curAttributes) {
        curAttributes.attributes.forEach(attribute => {
          if (it.attributes.find(a => a.id === attribute.id && !a.hidden)) {
            attribute.hidden = false;
          }
        });
      }
    });
  }

  async initCustomAttributes(hidden: boolean) {
    // 查找当前元类配置定制目标
    const propertyData = await modelService.getModelProperties(
      this.shapeCustomizationManagementModel.queryParams.projectId,
      this.shapeCustomizationManagementModel.queryParams.modelId
    );
    const targetValue: any[] = propertyData?.find(item => item.key === PropertyKey.MetaConfig_target)?.value as any[];

    if (!targetValue?.length) return;
    // 查找每个定制目标的自定义元素, 没有自定义元素则不添加
    const ids = targetValue.map(v => v.id).filter(Boolean);
    const attrs = await shapeService.queryStereotypeAttributes(this.shapeCustomizationManagementModel.queryParams.projectId, ids);
    this.customAttributeConfig.customAttributes = attrs
      .map(v => ({
        targetName: v.stereotypeName,
        targetModelId: v.stereotypeId,
        attributes: v.attributes.map(data => ({
          id: data.attributeId,
          name: data.attributeName,
          hidden: hidden
        }))
      }))
      .filter(config => config.attributes?.length);
  }

  initShapeCompCustomAttrConfig() {
    // this.customAttributeConfig = [];
  }
}
